package cn.nohan.funplay.module.member.mongo.entity;

import com.mongoplus.annotation.ID;
import com.mongoplus.annotation.collection.CollectionField;
import com.mongoplus.annotation.collection.CollectionName;
import com.mongoplus.annotation.index.MongoGeoIndex;
import com.mongoplus.annotation.index.MongoIndex;
import com.mongoplus.enums.GeoType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.index.*;
import org.springframework.data.mongodb.core.mapping.Document;


/**
 * @author caozhipeng
 * @date 2025/7/31
 * 用户轨迹表--永远保存最新经纬度
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@CollectionName("user_trajectory")
@Document(collection = "user_trajectory")
public class MongoUserTrajectoryEntity {


    @ID
    @Id
    private String id;

    @MongoIndex(unique = true)
    @CollectionField("userId")
    @Indexed(unique = true)
    private Long userId;

    @CollectionField("locationName")
    private String locationName;

    @GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE)
    @MongoGeoIndex(type = GeoType.SPHERE)
    @CollectionField("location")
    private GeoJsonPoint location;

    // 添加距离字段（不持久化）
    @Transient
    private Distance distance;

    @Transient
    private String range; // 新增的格式化距离字段




    public MongoUserTrajectoryEntity(Long userId,String locationName,double y,double x) {
        this.userId = userId;
        this.locationName=locationName;
        this.location = new GeoJsonPoint(x,y);
    }

    public MongoUserTrajectoryEntity(Long userId, GeoJsonPoint geoJsonPoint) {
        this.userId = userId;
        this.locationName=locationName;
        this.location = geoJsonPoint;
    }





    public void setRange(Distance calculatedDistance) {
//        this.calculatedDistance = calculatedDistance;
        // 自动设置格式化后的range值
        this.range = formatDistance(calculatedDistance);
    }



    // 距离格式化方法
    private String formatDistance(Distance distance) {
        if (distance == null) return "";

        double value = distance.getValue();
        if (distance.getMetric() == Metrics.KILOMETERS) {
            if (value >= 1) {
                return String.format("%.2fKM", value);
            } else {
                return String.format("%.2fM", value * 1000);
            }
        } else if (distance.getMetric() == Metrics.MILES) {
            // 如果需要处理英里单位
            if (value >= 1) {
                return String.format("%.2fMI", value);
            } else {
                return String.format("%.2fFT", value * 5280);
            }
        } else {
            // 默认处理为米
            if (value >= 1000) {
                return String.format("%.2fKM", value / 1000);
            } else {
                return String.format("%.2fM", value);
            }
        }
    }
}
